﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Bedroomss;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Departmentss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.PigFarmss
{

    public class ShowFarmHander : IRequestHandler<ShowFarmCommand, Result<PageResultDto<Pigfarmas>>>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Pigfarmas> pigfarmasRepository;

        public ShowFarmHander(IMapper mapper, IBaseRepository<Pigfarmas> pigfarmasRepository)
        {
            this.mapper = mapper;
            this.pigfarmasRepository = pigfarmasRepository;
        }

        public async Task<Result<PageResultDto<Pigfarmas>>> Handle(ShowFarmCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<Pigfarmas>>() { Code = (int)CommonErrorCode.Search };
            var page = new PageResultDto<Pigfarmas>() { List = new List<Pigfarmas>(), PageCount = 0, TotalCount = 0 };
            var res = await (await pigfarmasRepository.GetAll()).ToListAsync();
            if (!string.IsNullOrEmpty(request.FarmFullName))
            {
                res = res.Where(x => x.FarmFullName.Contains(request.FarmFullName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.FarmPro))
            {
                res = res.Where(x => x.FarmPro.Contains(request.FarmPro)).ToList();
            }
            page.TotalCount = res.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = res.OrderBy(x => x.FarmID).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = page;
            result.Code = (int)CommonErrorCode.Success;
            result.Msg = "查询成功";
            return result;

        }

        //public async Task<Result<PageResultDto<Pigfarmas>>> Handle(ShowFarmCommand request, CancellationToken cancellationToken)
        //{
        //    var result = new Result<PageResultDto<Pigfarmas>>() { Code = (int)CommonErrorCode.Search };
        //    var page = new PageResultDto<Pigfarmas>() { list = new List<Pigfarmas>(), pageCount = 0, totalCount = 0 };
        //    var res = await (await pigfarmasRepository.GetAll()).ToListAsync();
        //    if (!string.IsNullOrEmpty(request.FarmFullName))
        //    {
        //        res = res.Where(x => x.FarmFullName.Contains(request.FarmFullName)).ToList();
        //    }
        //    if (!string.IsNullOrEmpty(request.FarmPro))
        //    {
        //        res = res.Where(x => x.FarmPro.Contains(request.FarmPro)).ToList();
        //    }

        //    page.pageCount = (int)Math.Ceiling(page.totalCount * 1.0 / request.PageSize);
        //    page.totalCount = res.Count();
        //    page.list = res.OrderBy(x => x.FarmID).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
        //    result.Data = page;
        //    result.Code = (int)CommonErrorCode.Success;
        //    result.Msg = "查询成功";
        //    return result;
        //}
    }
}
